package com.pls.utilities;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class ConnectionManager {
	
	
	private static ConnectionManager instance = null;
	private static Connection con;

	/**
	 * private constructor of class for singleton
	 */
	private ConnectionManager() {
	}
	
	/**creates connection to database
	 * @return current connection
	 * @throws SQLException
	 * @throws Exception
	 * @throws ClassNotFoundException
	 */
	public Connection getConnection () throws SQLException, Exception, ClassNotFoundException {
			try {
				
				Context ctx = new InitialContext();
				DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/plsPool");
				con = ds.getConnection();

				
			} catch (SQLException sqlExc) {
				throw sqlExc;
			} catch (Exception e){
				throw e;
			}		

		return con;
	}
	
	/**for singleton
	 * @return current instance of class
	 */
	public static ConnectionManager getInstance() {
		
		if (instance == null) {
			instance = new ConnectionManager();
		}
		
		return instance;
	}
	
	public static void closeConnection() throws Exception{
		try{
			con.close();
		}catch (SQLException sqlExc) {
			throw sqlExc;
		} catch (Exception e){
			throw e;
		}	
	}
	
	
	


	

}
